package com.java.io.serializable;

import com.java.io.IOUtils;
import org.junit.Test;

import java.io.*;

/**
 * 作用:测试序列化和反序列化:将对象转换为字节流保存起来，并且在需要时还原回来。
 */
public class SerializeTest {

	@Test
	public void serialize(){
		Computer computer = new Computer(1,"E3",7000);
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream("source/Compuiter.ser"));
			out.writeObject(computer);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.close(out);
		}
	}

	@Test
	public void deserialize(){
		ObjectInputStream is = null;
		try {
			is = new ObjectInputStream(new FileInputStream("source/Compuiter.ser"));
			Computer computer = (Computer)is.readObject();
			System.out.println(computer);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.close(is);
		}
	}
}
